define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Classes, GoPage, $ionicScrollDelegate) {
		//左边选择
		$scope.leftIndex = 0;
		//画面加载
		$scope.$on('$ionicView.afterEnter', function(scope, status) {
			GoPage.init_top($scope, 44, 49);
		}, false);
		//加载数据
		$scope.init_leftdata = function() {
			//分类
			Classes.goodscattypeget(0, function(data) {
				//初始化33分类
				$scope.leftItems = data.category;
				$scope.init_rightdata(0);
			});
		};
		$scope.init_leftdata();
		$scope.init_rightdata = function(index) {
			//默认使用第一个
			$scope.leftIndex = index;
			//判断存在性
			if($scope.leftItems[index].right == undefined) {
				var logo = $scope.leftItems[index].logo;
				Classes.goodscattypeget($scope.leftItems[index].id, function(data) {
					for(var i = 0; i < data.category.length; i++) {
						data.category[i].logo = logo;
					}
					$scope.leftItems[index].right = data.category;
					$scope.rightItems = data.category;
				});
			} else {
				$scope.rightItems = $scope.leftItems[index].right;
			}
		};
		//点击分类切换
		$scope.activeClass = function(index) {
			// 设置移动
			$scope.leftIndex = index;
			$scope.init_rightdata(index);
			$ionicScrollDelegate.$getByHandle('calsses-handle').scrollTop(true);
		};
		//分类跳转检索
		$scope.onshow_search = function(cat) {
			GoPage.go_states("productsearch", {
				"top_search_flag": '0',
				"st": 'g',
				"keywords": "",
				"brand_id": 0,
				"cat_id": cat.id,
				"cat_name": cat.name
			});
		};
	}

	controller.$inject = ['$scope', 'Classes', 'GoPage', '$ionicScrollDelegate'];

	return app.registerController("classesctrl",controller);
});